package org.example.compare;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName FileComparator
 * @Description: 文件比较工具类
 * @Author YTH
 * @Date 2025-11-04 19:22
 */
public class FileComparator {

    private static final String TEST_RESULT_FILE_BASE = "../../test_result/"; // 基础路径
    // 修改比较结果文件的定义，增加默认值
    private static final String COMPARISON_RESULT_FILE = "../../test_result/comparison_results.txt"; // 比较结果输出文件

    /**
     * 比较两个结果文件是否相同
     *
     * @param i 用例编号
     * @return 如果两个文件内容相同返回true，否则返回false
     */
    public static boolean compareResultFiles(int i) {
        return compareResultFiles(TEST_RESULT_FILE_BASE, "capDateWorkTime", "capDateWorkTime_c", "random_case_", i);
    }

    /**
     * 比较两个手动测试用例文件是否相同
     *
     * @param i 用例编号
     * @return 如果两个文件内容相同返回true，否则返回false
     */
    public static boolean compareManualResultFiles(int i) {
        return compareResultFiles(TEST_RESULT_FILE_BASE, "capDateWorkTime", "capDateWorkTime_c", "manual_case_", i);
    }

    /**
     * 比较两个结果文件是否相同（可指定文件夹路径和文件前缀）
     *
     * @param baseDir 基础目录路径
     * @param folder1 第一个文件夹名称
     * @param folder2 第二个文件夹名称
     * @param filePrefix 文件名前缀
     * @param i 用例编号
     * @return 如果两个文件内容相同返回true，否则返回false
     */
    public static boolean compareResultFiles(String baseDir, String folder1, String folder2, String filePrefix, int i) {
        String file1Path = baseDir + folder1 + "/" + filePrefix + i + ".txt";
        String file2Path = baseDir + folder2 + "/" + filePrefix + i + ".txt";

        try {
            // 读取两个文件的所有行并比较
            var lines1 = Files.readAllLines(Paths.get(file1Path));
            var lines2 = Files.readAllLines(Paths.get(file2Path));

            // 比较行数是否相同
            if (lines1.size() != lines2.size()) {
                return false;
            }

            // 逐行比较内容
            for (int j = 0; j < lines1.size(); j++) {
                if (!lines1.get(j).equals(lines2.get(j))) {
                    return false;
                }
            }

            return true;
        } catch (IOException e) {
            System.err.println("读取文件时发生错误: " + e.getMessage());
            return false;
        }
    }

    /**
     * 将比较结果写入文件（使用默认文件名）
     *
     * @param results 比较结果列表，每个元素包含用例编号和比较结果
     */
    public static void writeResultsToFile(List<String> results) {
        writeResultsToFile(results, COMPARISON_RESULT_FILE);
    }

    /**
     * 将比较结果写入指定文件
     *
     * @param results 比较结果列表，每个元素包含用例编号和比较结果
     * @param resultFile 结果文件路径
     */
    public static void writeResultsToFile(List<String> results, String resultFile) {
        try {
            // 创建父目录（如果不存在）
            Files.createDirectories(Paths.get(resultFile).getParent());

            // 写入结果到文件
            Files.write(Paths.get(resultFile), results, StandardOpenOption.CREATE,
                    StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
        } catch (IOException e) {
            System.err.println("写入结果文件时发生错误: " + e.getMessage());
        }
    }

    @Test
    public void testCompareResultFiles() {
        // 测试随机用例
//        testCompareResultFiles("capDateWorkTime", "capDateWorkTime_c", "random_case_");
//        testCompareResultFiles("oneDayLeftSeconds", "oneDayLeftSeconds_c", "random_case_");
//        testCompareResultFiles("sameDayLeftSeconds", "sameDayLeftSeconds_c", "random_case_");
//        testCompareResultFiles("todayLeftSeconds", "todayLeftSeconds_c", "random_case_");
//        testCompareResultFiles("finalLeftSeconds", "finalLeftSeconds_c", "random_case_");
//        testCompareResultFiles("diffSeconds", "diffSeconds_c", "random_case_");
        testCompareResultFiles("calculateItem_1", "calculateItem_1_c", "random_case_");
        testCompareResultFiles("calculateItem_2", "calculateItem_2_c", "random_case_");
        testCompareResultFiles("calculateItem_3", "calculateItem_3_c", "random_case_");
        testCompareResultFiles("calculateItem_4", "calculateItem_4_c", "random_case_");
        testCompareResultFiles("calculateItem_5", "calculateItem_5_c", "random_case_");
        testCompareResultFiles("calculateItem_6", "calculateItem_6_c", "random_case_");
        testCompareResultFiles("calculateItem_7", "calculateItem_7_c", "random_case_");
        testCompareResultFiles("calculateItem_8", "calculateItem_8_c", "random_case_");
        testCompareResultFiles("calculateItem_9", "calculateItem_9_c", "random_case_");
        testCompareResultFiles("calculateItem_10", "calculateItem_10_c", "random_case_");
        testCompareResultFiles("calculateItem_12", "calculateItem_12_c", "random_case_");
        testCompareResultFiles("calculateItem_13", "calculateItem_13_c", "random_case_");
        testCompareResultFiles("calculateItem_14", "calculateItem_14_c", "random_case_");
//        testCompareResultFiles("reCheckCanSend_MinSTD", "reCheckCanSend_MinSTD_c", "random_case_");
//        testCompareResultFiles("recheckCanSend_WeiChi", "recheckCanSend_WeiChi_c", "random_case_");
//        testCompareResultFiles("isAllUndoInLibAndFew", "isAllUndoInLibAndFew_c", "random_case_");
//        testCompareResultFiles("recheckCanSend_QuanBuZaiKu", "recheckCanSend_QuanBuZaiKu_c", "random_case_");
//        testCompareResultFiles("calcuteTarget", "calcuteTarget_c", "random_case_");

        // 测试手动用例
//        testCompareResultFiles("capDateWorkTime", "capDateWorkTime_c", "manual_case_");
//        testCompareResultFiles("oneDayLeftSeconds", "oneDayLeftSeconds_c", "manual_case_");
//        testCompareResultFiles("sameDayLeftSeconds", "sameDayLeftSeconds_c", "manual_case_");
//        testCompareResultFiles("todayLeftSeconds", "todayLeftSeconds_c", "manual_case_");
//        testCompareResultFiles("finalLeftSeconds", "finalLeftSeconds_c", "manual_case_");
//        testCompareResultFiles("diffSeconds", "diffSeconds_c", "manual_case_");
        testCompareResultFiles("calculateItem_1", "calculateItem_1_c", "manual_case_");
        testCompareResultFiles("calculateItem_2", "calculateItem_2_c", "manual_case_");
        testCompareResultFiles("calculateItem_3", "calculateItem_3_c", "manual_case_");
        testCompareResultFiles("calculateItem_4", "calculateItem_4_c", "manual_case_");
        testCompareResultFiles("calculateItem_5", "calculateItem_5_c", "manual_case_");
        testCompareResultFiles("calculateItem_6", "calculateItem_6_c", "manual_case_");
        testCompareResultFiles("calculateItem_7", "calculateItem_7_c", "manual_case_");
        testCompareResultFiles("calculateItem_8", "calculateItem_8_c", "manual_case_");
        testCompareResultFiles("calculateItem_9", "calculateItem_9_c", "manual_case_");
        testCompareResultFiles("calculateItem_10", "calculateItem_10_c", "manual_case_");
        testCompareResultFiles("calculateItem_12", "calculateItem_12_c", "manual_case_");
        testCompareResultFiles("calculateItem_13", "calculateItem_13_c", "manual_case_");
        testCompareResultFiles("calculateItem_14", "calculateItem_14_c", "manual_case_");
//        testCompareResultFiles("reCheckCanSend_MinSTD", "reCheckCanSend_MinSTD_c", "manual_case_");
//        testCompareResultFiles("recheckCanSend_WeiChi", "recheckCanSend_WeiChi_c", "manual_case_");
//        testCompareResultFiles("isAllUndoInLibAndFew", "isAllUndoInLibAndFew_c", "manual_case_");
//        testCompareResultFiles("recheckCanSend_QuanBuZaiKu", "recheckCanSend_QuanBuZaiKu_c", "manual_case_");
//        testCompareResultFiles("calcuteTarget", "calcuteTarget_c", "manual_case_");
    }

    /**
     * 使用指定文件夹路径进行文件比较测试
     *
     * @param folder1 第一个文件夹名称
     * @param folder2 第二个文件夹名称
     * @param filePrefix 文件名前缀
     */
    public void testCompareResultFiles(String folder1, String folder2, String filePrefix) {
        // 根据文件夹名和文件前缀生成区分的结果文件名
        String prefixForFileName = filePrefix.replace("_", ""); // 去掉下划线以便生成合适的文件名
        String resultFileName = "../../test_result/comparison_results_" + folder1 + "_" + folder2 + "_" + prefixForFileName + ".txt";

        List<String> comparisonResults = new ArrayList<>();

        // 根据文件类型确定测试用例数量
        int testCaseCount = "manual_case_".equals(filePrefix) ? 2 : 50;

        for (int i = 1; i <= testCaseCount; i++) {
            boolean result = compareResultFiles(TEST_RESULT_FILE_BASE, folder1, folder2, filePrefix, i);
            String message = "两个文件" + filePrefix + i + (result ? "相同" : "不同");
            System.out.println(message);
            comparisonResults.add(message);
        }

        // 将结果写入文件
        writeResultsToFile(comparisonResults, resultFileName);
        System.out.println("比较结果已写入文件: " + resultFileName);
    }
}
